home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCollec / FWTMap.tpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  5.7 KB  |  177 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWTMap.tpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWTMap.h"
  11.  
  12. #ifndef FWERRORS_H
  13. #include "FWErrors.h"
  14. #endif
  15.  
  16. //========================================================================================
  17. // Class FW_TMap
  18. //========================================================================================
  19.  
  20. //----------------------------------------------------------------------------------------
  21. // FW_TMap<tKey, tValue>::FW_TMap
  22. //----------------------------------------------------------------------------------------
  23.  
  24. template<class tKey, class tValue>
  25. inline FW_TMap<tKey, tValue>::FW_TMap(FW_SortedArray_CompareFunction compare)
  26.     : fRep(0)
  27. {
  28.     FW_PlatformError error;
  29.     fRep = FW_PrivSortedArray_New(&error, compare);
  30.     FW_FailOnError(error);
  31. }
  32.  
  33. //----------------------------------------------------------------------------------------
  34. // FW_TMap<tKey, tValue>::FW_TMap
  35. //----------------------------------------------------------------------------------------
  36.  
  37. template<class tKey, class tValue>
  38. inline FW_TMap<tKey, tValue>::~FW_TMap()
  39. {
  40.     for (long i=GetLength()-1; i>=0; --i)
  41.     {
  42.         FW_TPair<tKey, tValue>* item = GetItemAt(i);
  43.         delete item;
  44.     }
  45.     FW_PrivSortedArray_Dispose(fRep);
  46. }
  47.  
  48. //----------------------------------------------------------------------------------------
  49. // FW_TMap<tKey, tValue>::GetLength
  50. //----------------------------------------------------------------------------------------
  51.  
  52. template<class tKey, class tValue>
  53. inline long FW_TMap<tKey, tValue>::GetLength() const
  54. {
  55.     return FW_PrivSortedArray_GetLength(fRep);
  56. }
  57.  
  58. //----------------------------------------------------------------------------------------
  59. // FW_TMap<tKey, tValue>::GetItemAt
  60. //----------------------------------------------------------------------------------------
  61.  
  62. template<class tKey, class tValue>
  63. inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::GetItemAt(long index) const
  64. {
  65.     return (FW_TPair<tKey, tValue>*) FW_PrivSortedArray_GetItemAt(fRep, index);
  66. }
  67.  
  68. //----------------------------------------------------------------------------------------
  69. // FW_TMap<tKey, tValue>::Find
  70. //----------------------------------------------------------------------------------------
  71.  
  72. template<class tKey, class tValue>
  73. inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Find(FW_TPair<tKey, tValue>* item) const
  74. {
  75.     FW_TPair<tKey, tValue>* result = 0;
  76.     long index;
  77.     FW_Boolean found = FW_PrivSortedArray_Find(fRep, item, &index);
  78.     if (found)
  79.         result = GetItemAt(index);
  80.     return result;
  81. }
  82.  
  83. //----------------------------------------------------------------------------------------
  84. // FW_TMap<tKey, tValue>::Find
  85. //----------------------------------------------------------------------------------------
  86.  
  87. template<class tKey, class tValue>
  88. inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Find(const tKey& key) const
  89. {
  90.     return Find(&FW_TPair<tKey, tValue>(key));
  91. }
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // FW_TMap<tKey, tValue>::Add
  95. //----------------------------------------------------------------------------------------
  96.  
  97. template<class tKey, class tValue>
  98. inline FW_TPair<tKey, tValue>*  FW_TMap<tKey, tValue>::Add(FW_TPair<tKey, tValue>* newItem)
  99. {
  100.     long index;
  101.     FW_PlatformError error;
  102.     FW_PrivSortedArray_Add(fRep, newItem, &index, &error);
  103.     FW_FailOnError(error);
  104.     return newItem;
  105. }
  106.  
  107. //----------------------------------------------------------------------------------------
  108. // FW_TMap<tKey, tValue>::Add
  109. //----------------------------------------------------------------------------------------
  110.  
  111. template<class tKey, class tValue>
  112. inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Add(const tKey& key, const tValue& value)
  113. {
  114.     FW_PlatformError error;
  115.     long index;
  116.     FW_TPair<tKey, tValue>* newItem = new FW_TPair<tKey, tValue>(key, value);
  117.     FW_PrivSortedArray_Add(fRep, newItem, &index, &error);
  118.     FW_FailOnError(error);
  119.     return newItem;
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. // FW_TMap<tKey, tValue>::Remove
  124. //----------------------------------------------------------------------------------------
  125.  
  126. template<class tKey, class tValue>
  127. inline void FW_TMap<tKey, tValue>::Remove(FW_TPair<tKey, tValue>* item)
  128. {
  129.     FW_PlatformError error;
  130.     long index;
  131.     FW_Boolean found = FW_PrivSortedArray_Find(fRep, item, &index);
  132.     if (found)
  133.     {
  134.         FW_TPair<tKey, tValue>* arrayItem = GetItemAt(index);
  135.         FW_PrivSortedArray_RemoveIndex(fRep, index, &error);
  136.         delete arrayItem;
  137.     }
  138.     else
  139.         error = FW_xBadRemove;
  140.         
  141.     FW_FailOnError(error);
  142. }
  143.  
  144. //----------------------------------------------------------------------------------------
  145. // FW_TMap<tKey, tValue>::Remove
  146. //----------------------------------------------------------------------------------------
  147.  
  148. template<class tKey, class tValue>
  149. void FW_TMap<tKey, tValue>::Remove(const tKey& key)
  150. {
  151.     Remove(&FW_TPair<tKey, tValue>(key));
  152. }
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // FW_TMap<tKey, tValue>::operator[]
  156. //----------------------------------------------------------------------------------------
  157.  
  158. template<class tKey, class tValue>
  159. inline tValue& FW_TMap<tKey, tValue>::operator[](const tKey& key)
  160. {
  161.     FW_PlatformError error;
  162.     FW_TPair<tKey, tValue>* item = 0;
  163.     long index;
  164.     FW_Boolean found = FW_PrivSortedArray_Find(fRep, &FW_TPair<tKey, tValue>(key), &index);
  165.     if (found)
  166.         item = GetItemAt(index);
  167.     else
  168.     {
  169.         item = new FW_TPair<tKey, tValue>(key);
  170.         FW_PrivSortedArray_Add(fRep, item, &index, &error);
  171.         FW_FailOnError(error);
  172.     }
  173.     FW_ASSERT(item != NULL);
  174.     return item->fValue;
  175. }
  176.  
  177.